package com.antaohua.platfrom.framework.base.dao;

import java.lang.reflect.Method;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.antaohua.platfrom.framework.base.utils.Condition;
import com.antaohua.platfrom.framework.base.interfase.CreateDateInterfase;
import com.antaohua.platfrom.framework.base.interfase.DeleteStateInterfase;
import com.antaohua.platfrom.framework.base.interfase.UpdateDateInterfase;
import com.antaohua.platfrom.framework.container.ThreeEntry;

public class BaseHibernateManager {

	private static Log log = LogFactory.getLog(BaseHibernateManager.class);
	
	
	
	
	public static <E> Boolean  isDeleteState(E entity) {
		return entity instanceof DeleteStateInterfase;
	}
	public static Boolean  isDeleteState(Class<?> entityClass) {
		return entityClass.isAssignableFrom(DeleteStateInterfase.class);
	}
	public static <E> Boolean  isUpdateDate(E entity) {
		return entity instanceof UpdateDateInterfase;
	}
	public static Boolean  isUpdateDate(Class<?> entityClass) {
		return entityClass.isAssignableFrom(UpdateDateInterfase.class);
	}
	public static <E> Boolean  isCreateDate(E entity) {
		return entity instanceof CreateDateInterfase;
	}
	public static Boolean  isCreateDate(Class<?> entityClass) {
		return entityClass.isAssignableFrom(CreateDateInterfase.class);
	}
	
	
	
	public static <E> E  updateEventAutoData(E entity) {
		if ((entity instanceof UpdateDateInterfase)) {
			((UpdateDateInterfase) entity).setUpdateDate(new Date());
		}
		return entity;
	}
	
	
	
	public static <E> E  createEventAutoData(E entity) {
		if (isCreateDate(entity)) {
			((CreateDateInterfase) entity).setCreateDate(new Date());
		}
		if (isDeleteState(entity)) {
			((DeleteStateInterfase) entity).setDeletedState(DeleteStateInterfase.DELETE_FALSE);
		}
		return entity;
	}
	
	
	
	
	public static <E> E  deleteEventAutoData(E entity) {
		if (isDeleteState(entity)) {
			((DeleteStateInterfase) entity).setDeletedState(DeleteStateInterfase.DELETE_TRUE);
		}
		if (isUpdateDate(entity)) {
			((UpdateDateInterfase) entity).setUpdateDate(new Date());
		}
		return entity;
	}
	
	
	public Criterion getCriterion(ThreeEntry<String, Condition, Object> property) {
		
		
		if(Condition.eq.equals(property.getCharacter())) {
			Restrictions.eq(property.getKey(), property.getValue());
		}else if(Condition.eqOrIsNull.equals(property.getCharacter())){
			Restrictions.eqOrIsNull(property.getKey(), property.getValue());
		}else if(Condition.ge.equals(property.getCharacter())){
			Restrictions.ge(property.getKey(), property.getValue());
		}else if(Condition.gt.equals(property.getCharacter())){
			Restrictions.gt(property.getKey(), property.getValue());
		}else if(Condition.in.equals(property.getCharacter())){
			Restrictions.in(property.getKey(), property.getValue());
		}else if(Condition.isNotNull.equals(property.getCharacter())){
			Restrictions.isNotNull(property.getKey());
		}else if(Condition.isNull.equals(property.getCharacter())){
			Restrictions.isNull(property.getKey());
		}else if(Condition.le.equals(property.getCharacter())){
			Restrictions.le(property.getKey(), property.getValue());
		}else if(Condition.like.equals(property.getCharacter())){
			Restrictions.like(property.getKey(), property.getValue());
		}else if(Condition.lt.equals(property.getCharacter())){
			Restrictions.lt(property.getKey(), property.getValue());
		}else if(Condition.ne.equals(property.getCharacter())){
			Restrictions.ne(property.getKey(), property.getValue());
		}else if(Condition.neOrIsNotNull.equals(property.getCharacter())){
			Restrictions.neOrIsNotNull(property.getKey(), property.getValue());
		}
		
		
		
		return null;
	}
	
	
	public static void main(String[] args) {
		Method[] method = Restrictions.class.getMethods();
		for (Method m : method) {
			System.out.println(m.getName());
		}
	}
	
}
